package com.coffee.test.server;

import com.alibaba.fastjson2.JSONObject;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;

import java.util.Map;

/**
 * @author songkui
 * @since 2024/6/12
 */
public class HJ212MsgUtils {
    private static final InternalLogger logger = InternalLoggerFactory.getInstance(HJ212MsgUtils.class);
    /**
     * 传入HJ212数据，返回简单json数据，没有对污染源进行分类汇总。
     * @param msg hj212
     * @return json
     */
    public static JSONObject dealMsg(String msg) {
        JSONObject data = new JSONObject();
        try {
            // 拆分消息
            String[] subMsg = msg.split("&&");

            // 清洗消息头基本数据
            String headStr = subMsg[0].substring(2).replace(";CP=", "").replace("=", "\":\"")
                    .replace(",", "\",\"").replace(";", "\",\"");
            data.put("SIZE", headStr.substring(0, 4));
            data.putAll(JSONObject.parseObject("{\"" + headStr.substring(4) + "\"}"));

            // 清洗数据体基本数据
            String paramStr = subMsg[1].replace("=", "\":\"").replace(",", "\",\"")
                    .replace(";", "\",\"");
            data.put("CP", JSONObject.parseObject("{\"" + paramStr + "\"}"));

            // 保存消息尾数据，主要是CRC校验和包结束符
            data.put("End", subMsg[2]);

        } catch (Exception e) {
            logger.error("HJ212 msg to json error>>> exception: {}， msg：{}", e.getMessage(), msg);
        }
        return data;
    }


    /**
     * 传入HJ212数据，返回复杂json数据，对污染源进行分类汇总，不同污染源用不同的json key。
     *
     * @param msg hj212
     * @return json
     */
    public static JSONObject dealMsgGroup(String msg) {
        JSONObject data = new JSONObject();
        try {
            // 拆分消息
            String[] subMsg = msg.split("&&");

            // 清洗消息头基本数据
            String headStr = subMsg[0].substring(2).replace(";CP=", "").replace("=", "\":\"")
                    .replace(",", "\",\"").replace(";", "\",\"");
            data.put("SIZE", headStr.substring(0, 4));
            data.putAll(JSONObject.parseObject("{\"" + headStr.substring(4) + "\"}"));

            // 清洗数据体基本数据
            String[] monitors = subMsg[1].split(";");
            JSONObject cp = new JSONObject();
            for (String obj : monitors) {
                String paramStr = obj.replace("=", "\":\"").replace(",", "\",\"")
                        .replace(";", "\",\"");
                // 如果是时间信息，则直接放到外层
                if (paramStr.contains("DataTime")) {
                    data.putAll(JSONObject.parseObject("{\"" + paramStr + "\"}"));
                } else {
                    String[] ele = getPollutionSource(paramStr);
                    cp.put(ele[0], JSONObject.parseObject("{\"" + ele[1] + "\"}"));
                }
            }
            data.put("CP", cp);

            // 保存消息尾数据，主要是CRC校验和包结束符
            data.put("End", subMsg[2]);

        } catch (Exception e) {
            logger.error("HJ212 msg to group json error>>> exception: {}， msg：{}", e.getMessage(), msg);
        }
        return data;
    }

    public static Map<String, Object> msgToGroupMap(String msg){
        JSONObject jsonObject = dealMsgGroup(msg);
        if (jsonObject.isEmpty()){
            return null;
        }

        return jsonObject.toJavaObject(Map.class);
    }


    /**
     * 解析污染源数据，获取污染源编号
     */
    private static String[] getPollutionSource(String data) {
        String key = data.substring(0, data.indexOf("-"));
        data = data.replaceAll(key + "-", "");

        String[] result = new String[2];
        result[0] = key;
        result[1] = data;
        return result;
    }
/*

//
    public static void main(String[] args) {
        System.out.println(dealMsg("##0746ST=31;CN=2061;PW=123456;MN=7568770259402;Flag=0;CP=&&DataTime=20221008100000;B02-Min=1.6960,B02-Avg=3.0586,B02-Max=3.7704,B02-Cou=11010.8437;S01-Min=17.7469,S01-Avg=19.4636,S01-Max=19.6944;S02-Min=3.2459,S02-Avg=5.6705,S02-Max=6.9578;S03-Min=30.0434,S03-Avg=30.2675,S03-Max=30.4503;S08-Min=-0.4643,S08-Avg=-0.3541,S08-Max=0.0000;S05-Min=6.1814,S05-Avg=6.8655,S05-Max=7.0097;a24088-Min=2.4957,a24088-Avg=3.2176,a24088-Max=4.7744,a24088-Cou=0.0354;25-Min=6.4292,25-Avg=12.1457,25-Max=20.0606,25-Cou=0.1336;a05002-Min=2.7715,a05002-Avg=7.8561,a05002-Max=14.7934,a05002-Cou=0.0863;17-Min=0.0000,17-Avg=0.0000,17-Max=0.0000,17-Cou=0.0000;18-Min=0.0000,18-Avg=0.0000,18-Max=0.0000,18-Cou=0.0000;16-Min=0.0000,16-Avg=0.0000,16-Max=0.0000,16-Cou=0.0000&&7BC0"));
        System.out.println(dealMsgGroup("##0746ST=31;CN=2061;PW=123456;MN=7568770259402;Flag=0;CP=&&DataTime=20221008100000;B02-Min=1.6960,B02-Avg=3.0586,B02-Max=3.7704,B02-Cou=11010.8437;S01-Min=17.7469,S01-Avg=19.4636,S01-Max=19.6944;S02-Min=3.2459,S02-Avg=5.6705,S02-Max=6.9578;S03-Min=30.0434,S03-Avg=30.2675,S03-Max=30.4503;S08-Min=-0.4643,S08-Avg=-0.3541,S08-Max=0.0000;S05-Min=6.1814,S05-Avg=6.8655,S05-Max=7.0097;a24088-Min=2.4957,a24088-Avg=3.2176,a24088-Max=4.7744,a24088-Cou=0.0354;25-Min=6.4292,25-Avg=12.1457,25-Max=20.0606,25-Cou=0.1336;a05002-Min=2.7715,a05002-Avg=7.8561,a05002-Max=14.7934,a05002-Cou=0.0863;17-Min=0.0000,17-Avg=0.0000,17-Max=0.0000,17-Cou=0.0000;18-Min=0.0000,18-Avg=0.0000,18-Max=0.0000,18-Cou=0.0000;16-Min=0.0000,16-Avg=0.0000,16-Max=0.0000,16-Cou=0.0000&&7BC0"));
        System.out.println(dealMsgGroup("##0949QN=20240613173308001;ST=31;CN=2051;PW=123456;MN=51152300QFLH01;Flag=6;PNUM=2;PNO=1;CP=&&DataTime=20240613173200;a00000-Cou=2521.0634,a00000-Min=41.0381,a00000-Avg=42.0177,a00000-Max=42.7233,a00000-Flag=N;a01011-Cou=2.9175,a01011-Min=2.8499,a01011-Avg=2.9175,a01011-Max=2.9700,a01011-Flag=N;a01012-Cou=101.7562,a01012-Min=101.5500,a01012-Avg=101.7562,a01012-Max=102.2249,a01012-Flag=N;a01013-Cou=-0.0200,a01013-Min=-0.0200,a01013-Avg=-0.0200,a01013-Max=-0.0200,a01013-Flag=N;a01014-Cou=1.7062,a01014-Min=1.7000,a01014-Avg=1.7062,a01014-Max=1.7100,a01014-Flag=N;a21002-Cou=0.0471,a21002-Min=18.7066,a21002-Avg=18.7066,a21002-Max=18.7066,a21002-ZsMin=37.2890,a21002-ZsAvg=37.2890,a21002-ZsMax=37.2890,a21002-Flag=C;a34013-Cou=0.0085,a34013-Min=3.3561,a34013-Avg=3.3759,a34013-Max=3.4341,a34013-ZsMin=6.6899,a34013-ZsAvg=6.7294,a34013-ZsMax=6.8454,a34013-Flag=C;a19001-Cou=14.9799,a19001-Min=14.9799,a19001-Avg=14.9799,a19001-Max=14.9799,a19001-Flag=C&&3E80"));
        System.out.println(dealMsgGroup("##0488QN=20240613173232001;ST=31;CN=2011;PW=123456;MN=51152300QFLH01;Flag=4;CP=&&DataTime=20240613173230;a00000-Rtd=41.9313,a00000-Flag=N;a01011-Rtd=2.9100,a01011-Flag=N;a01012-Rtd=101.5500,a01012-Flag=N;a01013-Rtd=-0.0200,a01013-Flag=N;a01014-Rtd=1.7100,a01014-Flag=N;a21002-Rtd=18.7066,a21002-ZsRtd=37.2890,a21002-Flag=C;a34013-Rtd=3.3561,a34013-ZsRtd=6.6899,a34013-Flag=C;a19001-Rtd=14.9799,a19001-Flag=C;a21026-Rtd=1.2200,a21026-ZsRtd=2.4318,a21026-Flag=C;a21003-Rtd=12.1999,a21003-Flag=C&&59C0"));



        String testData = "##0488QN=20240613173232001;ST=31;CN=2011;PW=123456;MN=51152300QFLH01;Flag=4;CP=&&DataTime=20240613173230;a00000-Rtd=41.9313,a00000-Flag=N;a01011-Rtd=2.9100,a01011-Flag=N;a01012-Rtd=101.5500,a01012-Flag=N;a01013-Rtd=-0.0200,a01013-Flag=N;a01014-Rtd=1.7100,a01014-Flag=N;a21002-Rtd=18.7066,a21002-ZsRtd=37.2890,a21002-Flag=C;a34013-Rtd=3.3561,a34013-ZsRtd=6.6899,a34013-Flag=C;a19001-Rtd=14.9799,a19001-Flag=C;a21026-Rtd=1.2200,a21026-ZsRtd=2.4318,a21026-Flag=C;a21003-Rtd=12.1999,a21003-Flag=C&&59C0";
        Map<String, Object> map = msgToGroupMap(testData);
        JSONObject js = dealMsgGroup(testData);
        test();
    }

    private static void test(){
        JSONObject jsonObject = dealMsgGroup("##0488QN=20240613173232001;ST=31;CN=2011;PW=123456;MN=51152300QFLH01;Flag=4;CP=&&DataTime=20240613173230;a00000-Rtd=41.9313,a00000-Flag=N;a01011-Rtd=2.9100,a01011-Flag=N;a01012-Rtd=101.5500,a01012-Flag=N;a01013-Rtd=-0.0200,a01013-Flag=N;a01014-Rtd=1.7100,a01014-Flag=N;a21002-Rtd=18.7066,a21002-ZsRtd=37.2890,a21002-Flag=C;a34013-Rtd=3.3561,a34013-ZsRtd=6.6899,a34013-Flag=C;a19001-Rtd=14.9799,a19001-Flag=C;a21026-Rtd=1.2200,a21026-ZsRtd=2.4318,a21026-Flag=C;a21003-Rtd=12.1999,a21003-Flag=C&&59C0");
       Map<String, Object> map = jsonObject.toJavaObject(Map.class);
        System.out.println(map.get("MN"));
    }
*/



}
